//
//  ZXSubscripitionView.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/8.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZXSubscripitionView.h"
#import "ZXLoginReigiserNavViewController.h"
#import "UIButton+JFAdd.h"

@implementation ZXSubscripitionView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor whiteColor];
    [self addSubview:button];
    
    [button setTitle:@"添加订阅" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.layer.cornerRadius = 5;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [button setImage:[UIImage imageNamed:@"top_addlike_n"] forState:UIControlStateNormal];
  
    [button addTarget:self action:@selector(subscripitionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.window.zx_centerX);
        make.top.equalTo(self.mas_top).offset(20);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(150);
    }];
    
    [button jf_layoutButtonWithEdgeInsetsStyle:JF_ButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    
    UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:loginBtn];
    [loginBtn setTitle:@"登录已有账号>>" forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [loginBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(didLoginButton) forControlEvents:UIControlEventTouchUpInside];
    
    [loginBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.window.zx_centerX);
        make.top.equalTo(button.mas_bottom).offset(5);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(120);
    }];
}

#pragma mark - button点击
- (void)subscripitionButtonClick:(UIButton *)btn {

    if ([self respondsToSelector:@selector(subscripitionButtonClick:)]) {
        [self.delegate subscripitionButtonClick:btn];
    }
}

- (void)didLoginButton {

    ZXLoginReigiserNavViewController * loginNavVC = [[ZXLoginReigiserNavViewController alloc] init];
    UIViewController * root = [self viewController];
    [root presentViewController:loginNavVC animated:YES completion:nil];
//    UIWindow * window = [[UIApplication sharedApplication].windows lastObject];
//    window.hidden = YES;
}

- (UIViewController *)viewController {
    for (UIView * next = [self superview]; next; next = next.superview) {
        UIResponder * nextResponser = [next nextResponder];
        if ([nextResponser isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponser;
        }
    }
    return nil;
}


@end
